//
// Created by REEJI2219 on 2024-08-09.
//
#include "SLinkList.h"
#include <iostream>
SNode::SNode() {
    this->nextCur=0;
    this->data=0;
}
SNode::~SNode() {
    this->nextCur=0;
    this->data=0;
}
SLinkList::SLinkList() {
    maxSize=100;
    length=0;
    array=new SNode[maxSize];
    linkList = new LinkList();
    headCur=0;
    for (int i=1;i<=maxSize;i++) {
        linkList->append(i);
    }
}
bool SLinkList::append(ElemType data) {
    SNode *node = new SNode();
    node->data=data;
    int cur=-1;
    if(!linkList->take(&cur)) {
        return false;
    }
    if(headCur==0) {
        headCur=cur;
        array[cur]=*node;
    }else {
        int current = headCur;
        while (array[current].nextCur!=0) {
            current = array[current].nextCur;
        }
        array[cur]=*node;
        array[current].nextCur = cur;
    }
    return true;
}
void SLinkList::printList() {
    int currentHead = headCur;
    printf("headIndex:{%d}\n",headCur);
    while (array[currentHead].nextCur!=0) {
        printf("currendId:{%d} value{%d} nextId:{%d} \n",currentHead,array[currentHead].data,array[currentHead].nextCur);
        currentHead = array[currentHead].nextCur;
    }
    printf("currendId:{%d} value{%d} nextId:{%d} \n\n",currentHead,array[currentHead].data,array[currentHead].nextCur);
}

bool SLinkList::remove(int i,ElemType *data) {
    if(i<=0) {
        return false;
    }
    if(i==headCur) {
        *data=array[headCur].data;
        linkList->insert(0,headCur);
        headCur=array[headCur].nextCur;
    }else {
        int currentCur = this->headCur;
        while (array[currentCur].nextCur!=i) {
            currentCur=array[currentCur].nextCur;
            if(currentCur==0) {
                *data=0;
                return false;
            }
        }
        *data=array[currentCur].nextCur;
        if(array[currentCur].nextCur!=0) {
            array[currentCur].nextCur  = array[array[currentCur].nextCur].nextCur;
        }else {
            array[currentCur].nextCur=0;
        }

    }
    return true;
}
bool SLinkList::uniList(SLinkList *other, SLinkList *result) {
    //当前存在，other不存在。放到result
    int currentHead = this->headCur;
    //(A-B)U(B-A) = (A-(A n B))U (B-(A n B))
    while (array[currentHead].nextCur!=0) {
        int otherCur=0;
        other->find(array[currentHead].data,&otherCur);
        //a存在，b不存在
        if(otherCur!=0) {
            ElemType daleteElement;
            other->remove(otherCur,&daleteElement);
            this->remove(currentHead,&daleteElement);
        }
        currentHead = array[currentHead].nextCur;
    }
    result->appendList(this);
    result->appendList(other);
    return true;
}
bool SLinkList::appendList(SLinkList *other) {
    int otherCurl = other->headCur;
    while (other->array[otherCurl].nextCur!=0) {
        this->append(other->array[otherCurl].data);
        otherCurl = other->array[otherCurl].nextCur;
    }
    this->append(other->array[otherCurl].data);
}


bool SLinkList::find(ElemType data,int *position) {
    int currentCur = this->headCur;
    while (currentCur!=0) {
        if(array[currentCur].data!=data) {
            currentCur=array[currentCur].nextCur;
        }else {
            *position=currentCur;
            return true;
        }
    }
    *position=0;
    return false;
}
SLinkList::~SLinkList() {
    delete[] array;
}



